package com.itstyle.seckill.topic.java8;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.time.Month;
import java.util.Arrays;
import java.util.IntSummaryStatistics;
import java.util.List;
import java.util.Optional;
import java.util.stream.Collectors;

/**
 * @Auther: liuwenxiang
 * @Date: 2020/4/4 11:51
 * @Description:
 *
 * java8新特性：
 *  集合数据的处理；--lamda,stream,::
 *  对数据包装； --option
 *  当前时间，时区处理；--localtime
 *
 */
public class StreamTest {


    public static void main(String[] args) {



        time();





    }

    public static  void time(){

        /**
         * ZonedDateTime 时区
         * LocalDateTime 日期时间
         * LocalDate 日期
         * LocalTime 时间
         */
        LocalDateTime currentTime = LocalDateTime.now();

        System.out.println("当前时间: " + currentTime);
        LocalDate date1 = currentTime.toLocalDate();
        System.out.println("date1: " + date1);

        Month month = currentTime.getMonth();
        int day = currentTime.getDayOfMonth();
        int seconds = currentTime.getSecond();
        System.out.println("月: " + month + ", 日: " + day + ", 秒: " + seconds);

        LocalDateTime date2 = currentTime.withDayOfMonth(10).withYear(2012);
        System.out.println("date2: " + date2);
        // 12 december 2014
        LocalDate date3 = LocalDate.of(2014, Month.DECEMBER, 12);
        System.out.println("date3: " + date3);
        // 22 小时 15 分钟
        LocalTime date4 = LocalTime.of(22, 15);
        System.out.println("date4: " + date4);
        // 解析字符串
        LocalTime date5 = LocalTime.parse("20:15:30");
        System.out.println("date5: " + date5);

    }



    public static void option(){
        Integer value1 = null;
        Integer value2 = new Integer(10);

        Optional<Integer> a = Optional.ofNullable(value2);
        a.get();
        //a.flatMap(t->t+2).get();

    }

    public  static  void stream(){
        List<String> strings = Arrays.asList("abc", "", "bc", "efg", "abcd", "", "jkl");
        List<Integer> numbers = Arrays.asList(3, 2, 2, 3, 7, 3, 5);
        List<Integer> integers = Arrays.asList(1, 2, 13, 4, 15, 6, 17, 8, 19);



        long count = strings.stream().filter((x)->x.isEmpty()).count();
        System.out.println("空字符串的数量："+count);
        count = strings.stream().filter(x->x.length()==3).count();
        System.out.println("字符串长度为3的个数："+count);
        List filtered = strings.stream().filter(x->!x.isEmpty()).collect(Collectors.toList());
        System.out.println("筛选后的列表: " +filtered);
        String mergedString = strings.stream().filter(x->!x.isEmpty()).collect(Collectors.joining(", "));
        System.out.println("合并字符串: " + mergedString);

        List squaresList = numbers.stream().map(i -> i * i).distinct().collect(Collectors.toList());
        System.out.println("Squares List: " + squaresList);
        IntSummaryStatistics stats = integers.stream().mapToInt(x->x).summaryStatistics();
        System.out.println("列表中最大的数 : " + stats.getMax());
    }
}
